package com.neusoft.gene.nep.grid.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.gene.nep.entity.CommonResult;
import com.neusoft.gene.nep.grid.mapper.GridMapper;
import com.neusoft.gene.nep.grid.service.IGridService;
import com.neusoft.gene.nep.entity.ResultCode;
import com.neusoft.gene.nep.grid.entity.Grid;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: TODO   区域网格信息 服务实现类
 * 处理区域网格信息的增删改查操作
 * @author: 君醉
 * @date: 2024/6/19 15:14
*/

@Service
public class GridServiceImpl extends ServiceImpl<GridMapper, Grid> implements IGridService {
    @Autowired
    private GridMapper gridMapper;

    @Override
    // 添加一个区域网格信息
    public CommonResult addGrid(Grid grid) {
        try {
            //是否成功保存数据
            boolean b = save(grid);
            if (b) {
                // 返回成功
                return CommonResult.success(grid.getId());
            }
            else {
                // 返回失败
                return CommonResult.failed(ResultCode.INSERT_FAILED);
            }
        } catch (Exception ex) {
            // 返回异常信息
            return CommonResult.failed(500, ex.getMessage());
        }

    }
    @Override
    //根据父级区域网格ID查询下级区域网格信息
    public CommonResult selectGridByPid(Integer pid) {
        // 查询数据
        LambdaQueryWrapper<Grid> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 查询条件
        lambdaQueryWrapper.eq( Grid::getPid , pid   ).eq(Grid::getStatus, 1);
        List<Grid> list = list(lambdaQueryWrapper);
        if (list ==null ||  list.size() ==0){
            // 返回失败
            return   CommonResult.failed(ResultCode.RESULTSET_IS_EMPTY);
        }
        // 返回成功
        return CommonResult.success(list);
    }

    @Override
    //区域网格ID数组查询区域网格信息
    public CommonResult selectGridByIds(int[] is) {
        LambdaQueryWrapper<Grid> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Integer> l = Arrays.stream(is).boxed().collect(Collectors.toList());
        //查询ID在数组中的网格信息，并按照网格名称倒序排序。
        lambdaQueryWrapper.in( Grid::getId , l ).orderByDesc( Grid::getGname);
        List<Grid> list = list(lambdaQueryWrapper);

        if (list ==null ||  list.size() ==0){
            return   CommonResult.failed(ResultCode.RESULTSET_IS_EMPTY);
        }
        return CommonResult.success(list);
    }

    @Override
    // 分页查询区域网格信息
    public CommonResult selectPagedGrid(long current, long size) {
        // 创建分页对象
        IPage<Grid>  pagec =new Page<>(  current,   size);
        LambdaQueryWrapper<Grid>  queryWrapper =new LambdaQueryWrapper<>();
        //查询ID大于等于6的网格信息
        queryWrapper.ge( Grid::getId , 6  );
        IPage<Grid> pages = getBaseMapper().selectPage(pagec, queryWrapper);
        return CommonResult.success(pages);
    }

    @Override
    //查询所有区域网格信息并构建成树形结构
    public CommonResult<Grid> selectAllGridTree() {
        LambdaQueryWrapper<Grid> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        int pid = 0;
        //首先查询根节点（父级ID为0的节点），然后递归填充子节点，直到节点层级达到3层或没有子节点为
        lambdaQueryWrapper.eq( Grid::getPid , pid   );
        // 查询根节点
        Grid root =  getBaseMapper().selectOne(  lambdaQueryWrapper );
        fillChildren(root);
        return CommonResult.success(root);
    }

    //填充树形结构的子节点
    private void fillChildren(Grid root) {
        int  pid =  root.getId();
        LambdaQueryWrapper<Grid> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq( Grid::getPid , pid   );
        root.setChildren(   getBaseMapper().selectList(lambdaQueryWrapper));
        //根据父节点的ID查询其子节点信息，并递归调用 fillChildren 方法填充子节点的子节点，直到达到最大层级或没有子节点为止。
        if( root.getLevel() >=3 ||  root.getChildren() == null ||  root.getChildren().size() ==0 ){
            return;
        }
        if( root.getLevel() < 3   ){
            root.getChildren().forEach(  (grid)->{
                fillChildren(grid);
            });
        }
    }

    // 查询网格信息，并按照网格名称倒序排序。
    public CommonResult selectGridByPid1(Integer pid) {
         List<Grid> lgs =  gridMapper.selectGridByPid(pid);
         if (lgs ==null ||  lgs.size() ==0){
             return   CommonResult.failed(ResultCode.RESULTSET_IS_EMPTY);
         }
        return CommonResult.success(lgs);
    }
}
